﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Serialization;
using System.IO.IsolatedStorage;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace Chianti.Models
{
    public class DealCollection : INotifyPropertyChanged
    {
        private static ObservableCollection<Deal> _deals = new ObservableCollection<Deal>();
        public ObservableCollection<Deal> Deals
        {
            get { return _deals; }
        }

        private int _id = 0;
        public int ID
        {
            get { return _id; }
            set
            {
                _id = value;
                NotifyPropertyChanged("ID");
            }
        }

        private string _title = string.Empty;
        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                NotifyPropertyChanged("Title");
            }
        }

        private string _description = string.Empty;
        public string Description
        {
            get { return _description; }
            set
            {
                _description = value;
                NotifyPropertyChanged("Description");
            }
        }

        private string _name = string.Empty;
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }

        private float _longitude = 0;
        public float Longitude
        {
            get { return _longitude; }
            set
            {
                _longitude = value;
                NotifyPropertyChanged("Longitude");
            }
        }

        private float _latitude = 0;
        public float Latitude
        {
            get { return _latitude; }
            set
            {
                _latitude = value;
                NotifyPropertyChanged("Latitude");
            }
        }

        private int _radius = 0;
        public int Radius
        {
            get { return _radius; }
            set
            {
                _radius = value;
                NotifyPropertyChanged("Radius");
            }
        }


        public static void SaveDeal(Deal deal, string fileName)
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream fs = isf.CreateFile(fileName))
                {
                    XmlSerializer ser = new XmlSerializer(typeof(Deal));
                    ser.Serialize(fs, deal);
                }
            }
        }


        public class Deal
        {
            public int ID { get; set; }
            public string Title { get; set; }
            public string Description { get; set; }
            public string Name { get; set; }
            public float Longitude { get; set; }
            public float Latitude { get; set; }
            public int Radius { get; set; }

            public Deal(
                int _id,
                string _title,
                string _description,
                string _name,
                float _longitude,
                float _latitude,
                int _radius)
            {
                ID = _id;
                Title = _title;
                Description = _description;
                Name = _name;
                Longitude = _longitude;
                Latitude = _latitude;
                Radius = _radius;
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if (null != PropertyChanged)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }


        //public Deal()
        //{
        //    Uri uri = new Uri("http://localhost/chiantiweb/handler.ashx");
        //    WebClient webClient = new WebClient();
        //    webClient.DownloadStringCompleted += OnDownloadStringCompleted;
        //    webClient.DownloadStringAsync(uri);
        //}

        //void OnDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs args)
        //{
        //        StringReader reader = new StringReader(args.Result);
        //        XmlSerializer xml = new XmlSerializer(typeof(Deal));
        //        business_name = xml.Deserialize(reader) as String;
        //        short_description = xml.Deserialize(reader) as String;
        //}
    }
}
